package cn.slipi.admin.biz;

import cn.slipi.admin.biz.vo.producer.DropVo;
import cn.slipi.admin.biz.vo.producer.ProducerVo;
import cn.slipi.admin.common.dto.PageDTO;
import cn.slipi.admin.common.utils.dozer.impl.ConvertBean;
import cn.slipi.admin.controller.form.producer.ListForm;
import cn.slipi.admin.controller.form.producer.SaveForm;
import cn.slipi.admin.entity.PdrProject;
import cn.slipi.admin.service.PdrProjectService;
import cn.slipi.admin.service.dto.in.producer.*;
import cn.slipi.admin.service.dto.out.ProjectUpdateDTO;
import cn.slipi.admin.task.PdrApiTask;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class ProducerBiz {

    @Autowired
    private PdrApiTask pdrApiTask;

    @Autowired
    private ConvertBean convertBean;

    @Autowired
    private PdrProjectService projectService;

    public List<DropVo> getDropList() {
        List<PdrProject> projectList = this.projectService.getDropList();
        return this.convertBean.convert(projectList, DropVo.class);
    }

    public PageDTO<ProducerVo> getList(ListForm listForm) {
        ListDTO listDTO = this.convertBean.convert(listForm, ListDTO.class);
        IPage<PdrProject> page = this.projectService.getList(listDTO);
        List<PdrProject> pdrProjectList = page.getRecords();
        List<ProducerVo> producerVoList = new ArrayList<>();
        pdrProjectList.forEach(pdrProject -> {
            producerVoList.add(this.beanConvert(pdrProject));
        });
        return PageDTO.of(page, producerVoList);
    }

    public ProducerVo add(SaveForm saveForm) {
        SaveDTO saveDTO = this.convertBean.convert(saveForm, SaveDTO.class);
        PdrProject project = this.projectService.add(saveDTO);
        return this.beanConvert(project);
    }

    public ProducerVo update(Integer id, SaveForm saveForm) {
        SaveDTO saveDTO = this.convertBean.convert(saveForm, SaveDTO.class);
        ProjectUpdateDTO projectUpdateDTO = this.projectService.update(id, saveDTO);
        //更新关联的api接口配置
        if (projectUpdateDTO.getKeyUpdate()) {
            this.pdrApiTask.update(id, projectUpdateDTO.getKeyUpdateInfo());
        }
        return this.beanConvert(projectUpdateDTO.getPdrProject());
    }

    public ProducerVo detail(Integer id) {
        PdrProject project = this.projectService.detail(id);
        return this.beanConvert(project);
    }

    private ProducerVo beanConvert(PdrProject pdrProject) {
        ProducerVo producerVo = new ProducerVo();
        producerVo.setId(pdrProject.getId());
        producerVo.setName(pdrProject.getName());
        producerVo.setDescription(pdrProject.getDescription());
        producerVo.setLoadIp(pdrProject.getLoadIp());
        producerVo.setEnable(pdrProject.getEnable());
        producerVo.setProtocol(pdrProject.getProtocol());
        producerVo.setTranslateProtocol(pdrProject.getTranslateProtocol());
        producerVo.setLoadBalance(JSONObject.parseObject(pdrProject.getLoadBalance(), LoadBalance.class));
//        producerVo.setHystrixParam(JSONObject.parseObject(pdrProject.getHystrixParam(), HystrixParam.class));
        producerVo.setGrayRelease(JSONObject.parseObject(pdrProject.getGrayRelease(), GrayRelease.class));
        producerVo.setCreatedAt(pdrProject.getCreatedAt());
        return producerVo;
    }

}
